package com.xxx.zkconfig.helper;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.Map;

/**
 * Created by louisyuu on 2020/4/30 下午5:28
 */


@RestController
@RequestMapping("config")
public class ConfigController {
    @Autowired
    private DruidDataSource dataSource;


    @RequestMapping("update")
    @ResponseBody
    public String update(String database) {
        Map<String, Object> map = ZkUtils.readConfigToMap();
        map.put("spring.datasource.url", "jdbc:mysql://47.92.208.251:3306/" + database + "?serverTimezone=UTC");
        ZkUtils.updateConfig(map);
        return "Database switched to " + database + " successfully";
    }


    @RequestMapping("getDataSourceConfig")
    @ResponseBody
    public String getDataSourceConfig() {
        return dataSource.getUrl();
    }


}
